home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Info2.aspx.cs497 < prev    next >
Text File  |  2008-04-01  |  16KB  |  449 lines

  1. //===========================================================================
  2. // This file was modified as part of an ASP.NET 2.0 Web project conversion.
  3. // The class name was changed and the class modified to inherit from the abstract base class 
  4. // in file 'App_Code\Migrated\Stub_Info_aspx_cs.cs'.
  5. // During runtime, this allows other classes in your web application to bind and access 
  6. // the code-behind page using the abstract base class.
  7. // The associated content page 'Info.aspx' was also modified to refer to the new class name.
  8. // For more information on this code pattern, please refer to http://go.microsoft.com/fwlink/?LinkId=46995 
  9. //===========================================================================
  10.  
  11. using System;
  12. using System.Collections;
  13. using System.Data;
  14. using System.Data.Common;
  15. using System.IO;
  16. using System.Management;
  17. using System.Runtime.InteropServices;
  18. using System.Web.UI.HtmlControls;
  19. using System.Web.UI.WebControls;
  20. using GBPVR.Public;
  21. using GBPVRSchedule;
  22. using gbweb.classes;
  23. // For IRecordingService
  24.  
  25. namespace gbweb
  26. {
  27.     /// <summary>
  28.     /// Summary description for Info.
  29.     /// </summary>
  30.     public partial class Migrated_Info2 : Info
  31.     {
  32.  
  33.         private Settings guideParams;
  34.         
  35.         protected void Page_Load(object sender, EventArgs e)
  36.         {
  37.             guideParams = Global.Settings;
  38.  
  39.             //Code to prevent Guest users from accessing the Admin and search Pages
  40.             if (!Convert.ToBoolean((string)Session["NotGuestUser"]))
  41.             {
  42.                 NAV_SEARCH.Visible = false;
  43.                 NAV_CONFIG.Visible = false;
  44.             }
  45.  
  46.             if (!IsPostBack)
  47.             {
  48.                 //Set the Day/date/time display on the header
  49.                 viewDate.InnerText = DateTime.Now.ToLongDateString().Trim();
  50.                 serverTime.InnerText = "Server Time: " + DateTime.Now.ToLongTimeString().Trim();
  51.  
  52.                 //Set the header tab of the Statistics to active
  53.                 NAV_STATS.Attributes.Add("class", "currentTab");
  54.                 NAV_RECORDINGS.Visible = guideParams.showManage;
  55.                 if (Convert.ToBoolean((string)Session["NotGuestUser"]))
  56.                 {
  57.                     NAV_SEARCH.Visible = guideParams.showSearch;
  58.                 }
  59.                 NAV_VIDEO.Visible = guideParams.showVideoLib;
  60.                 NAV_MUSIC.Visible = guideParams.showMusicLib;
  61.                 NAV_PHOTO.Visible = guideParams.showPhotoLib;
  62.                 NAV_STATS.Visible = guideParams.showStats;
  63.             }
  64.  
  65.             Session["GuideStartTime"] = null;
  66.             GetDiskInformation("RecordingsDirectory", lblRecordingStats, imgRecordingUsedSystem, imgRecordingUsedGBPVR, imgRecordingFree);
  67.             GetDiskInformation("LiveTVDirectory", lblBufferStats, imgBufferUsedSystem, imgBufferUsedGBPVR, imgBufferFree);
  68.             hlLogFile.Text = AppDomain.CurrentDomain.FriendlyName + ".log";
  69.             hlLogFile.NavigateUrl = Download.GetDownloadUrl(false, true, Download.InternalFiles.Log);
  70.  
  71.             Schedule scheduleHelper = Global.Schedule;
  72.             IList myScheduledRecordings = scheduleHelper.LoadRecordingSchedule();
  73.             lblScheduleStatus.Text =
  74.                 GetScheduleStatus(myScheduledRecordings, "Pending", ScheduledRecording.STATUS_PENDING) + ", " +
  75.                 GetScheduleStatus(myScheduledRecordings, "In Progress", ScheduledRecording.STATUS_IN_PROGRESS) + ", " +
  76.                 GetScheduleStatus(myScheduledRecordings, "Available", ScheduledRecording.STATUS_COMPLETED) + ", " +
  77.                 GetScheduleStatus(myScheduledRecordings, "Failed", ScheduledRecording.STATUS_COMPLETED_WITH_ERROR) + ", " +
  78.                 GetScheduleStatus(myScheduledRecordings, "Conflict", ScheduledRecording.STATUS_CONFLICT) + ", " +
  79.                 GetScheduleStatus(myScheduledRecordings, "Reoccurring", ScheduledRecording.STATUS_PLACE_HOLDER) + ", " +
  80.                 GetScheduleStatus(myScheduledRecordings, "Deleted", ScheduledRecording.STATUS_DELETED);
  81.  
  82.             try
  83.             {
  84.                 GetRecordingStatuses();
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.                 Logger.Error(ex.ToString());
  89.                 Response.Write("<!--\n" + ex.ToString() + "\n-->");
  90.             }
  91.  
  92.             // create the database connection
  93.             using (DbConnection aConnection = Global.GetOpenGBPVRDbConnection())
  94.             {
  95.                 try
  96.                 {
  97.                     lblEPGRemaining.Text = GetEPGRemaining(aConnection);
  98.                 }
  99.                 catch (Exception ex)
  100.                 {
  101.                     Logger.Error(ex.ToString());
  102.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  103.                 }
  104.  
  105.                 try
  106.                 {
  107.                     lblCurrentTranscode.Text = GetCurrentTranscode(aConnection);
  108.                 }
  109.                 catch (Exception ex)
  110.                 {
  111.                     Logger.Error(ex.ToString());
  112.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  113.                 }
  114.  
  115.                 //close the connection
  116.                 aConnection.Close();
  117.             }
  118.  
  119.         }
  120.  
  121.         #region Disk Information
  122.         private void GetDiskInformation(string nodeName, Label lblStatus, HtmlImage imgUsedSystem, HtmlImage imgUsedGBPVR, HtmlImage imgFree)
  123.         {
  124.             string pathName = Global.Config.SelectSingleNode("/settings/" + nodeName).InnerText;
  125.  
  126.             string scope;
  127.             string root;
  128.             ulong FreeBytesAvailable = 0;
  129.             ulong TotalNumberOfBytes = 0;
  130.             ulong TotalNumberOfFreeBytes = 0;
  131.             bool nonSharedMap = false;
  132.  
  133.             double size = 0;
  134.             double free = 0;
  135.  
  136.             if (pathName.StartsWith(@"\\"))
  137.             {
  138.                 scope = pathName.Substring(0, pathName.IndexOf(@"\", 2));
  139.                 root = Directory.GetDirectoryRoot(pathName);
  140.                 string shareName = root.Substring(scope.Length + 1);
  141.  
  142.                 bool success = GetDiskFreeSpaceEx(root, out FreeBytesAvailable, out TotalNumberOfBytes,
  143.                                    out TotalNumberOfFreeBytes);
  144.                 if (!success)
  145.                 {
  146.                     Logger.Info("Falling through to wmi for free space determination.....");
  147.                     //throw new System.ComponentModel.Win32Exception();
  148.                     ManagementObject share = new ManagementObject(scope + @"\root\cimv2", "Win32_Share.Name=\"" + shareName + "\"", null);
  149.                     try
  150.                     {
  151.                         share.Get();
  152.                     }
  153.                     catch (Exception ex)
  154.                     {
  155.                         lblStatus.Text = "Could not load share info for " + root;
  156.                         imgFree.Visible = false;
  157.                         imgUsedGBPVR.Visible = false;
  158.                         imgUsedSystem.Visible = false;
  159.                         Logger.Warning(ex.ToString());
  160.                         share.Dispose();
  161.                         return;
  162.                     }
  163.                 
  164.                     try
  165.                     {
  166.                         root = share["Path"].ToString();
  167.                     }
  168.                     catch (Exception ex)
  169.                     {
  170.                         lblStatus.Text = "Could not load share info for " + root;
  171.                         imgFree.Visible = false;
  172.                         imgUsedGBPVR.Visible = false;
  173.                         imgUsedSystem.Visible = false;
  174.                         Logger.Warning(ex.ToString());
  175.                         share.Dispose();
  176.                         return;
  177.                     }
  178.                     share.Dispose();
  179.                 }
  180.                 else
  181.                 {
  182.                     Logger.Info("Using native win32 call for free space determination.....");
  183.                     nonSharedMap = true;
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 Logger.Info("Using wmi for free space determination.....");
  189.                 scope = @"\\.";
  190.                 root = Directory.GetDirectoryRoot(pathName);
  191.             }
  192.  
  193.             if (!nonSharedMap)
  194.             {
  195.                 root = root.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
  196.  
  197.                 ManagementObject disk = new ManagementObject(scope + @"\root\cimv2", "Win32_LogicalDisk.DeviceID=\"" + root + "\"", null);
  198.  
  199.                 try
  200.                 {
  201.                     disk.Get();
  202.                 }
  203.                 catch (Exception ex)
  204.                 {
  205.                     lblStatus.Text = "Could not load disk info for " + Directory.GetDirectoryRoot(pathName);
  206.                     imgFree.Visible = false;
  207.                     imgUsedGBPVR.Visible = false;
  208.                     imgUsedSystem.Visible = false;
  209.                     Logger.Warning(ex.ToString());
  210.                     disk.Dispose();
  211.                     return;
  212.                 }
  213.                 size = Convert.ToInt64(disk["Size"]);
  214.                 free = Convert.ToInt64(disk["FreeSpace"]);
  215.                 disk.Dispose();
  216.             }
  217.             else
  218.             {
  219.                 size = Convert.ToInt64(TotalNumberOfBytes);
  220.                 free = Convert.ToInt64(FreeBytesAvailable);
  221.             }
  222.  
  223.             double usedgbpvr = GetDirectorySize(pathName);
  224.             double usedsystem = size - free - usedgbpvr;
  225.  
  226.             double freepercent = ((double)free/(double)size) * 100.0;
  227.             double usedgbpvrpercent = ((double)usedgbpvr/(double)size) * 100.0;
  228.             double usedsystempercent = ((double)usedsystem/(double)size) * 100.0;
  229.  
  230.             string sizeunit = "B";
  231.             string freeunit = "B";
  232.             string usedgbpvrunit = "B";
  233.             string usedsystemunit = "B";
  234.  
  235.             ReduceSIUnit(ref size, ref sizeunit);
  236.             ReduceSIUnit(ref free, ref freeunit);
  237.             ReduceSIUnit(ref usedgbpvr, ref usedgbpvrunit);
  238.             ReduceSIUnit(ref usedsystem, ref usedsystemunit);
  239.  
  240.             lblStatus.Text = Directory.GetDirectoryRoot(pathName) + " " +
  241.                 size.ToString("0.0") + " " + sizeunit + " Total, " +
  242.                 free.ToString("0.0") + " " + freeunit + " Free, " +
  243.                 freepercent.ToString("#0.00") + "% Free";
  244.  
  245.             double barlength = 300;
  246.             imgFree.Width = (int)(freepercent * barlength / 100);
  247.             imgFree.Visible = imgFree.Width > 0;
  248.             imgUsedGBPVR.Width = (int)(usedgbpvrpercent * barlength / 100);
  249.             imgUsedGBPVR.Visible = imgUsedGBPVR.Width > 0;
  250.             imgUsedSystem.Width = (int)(usedsystempercent * barlength / 100);
  251.             imgUsedSystem.Visible = imgUsedSystem.Width > 0;
  252.  
  253.             imgUsedSystem.Attributes["title"] =
  254.                 usedsystem.ToString("0.0") + " " + usedsystemunit + " Used (System), " +
  255.                 usedsystempercent.ToString("#0.00") + "%";
  256.             imgUsedGBPVR.Attributes["title"] =
  257.                 usedgbpvr.ToString("0.0") + " " + usedgbpvrunit + " Used (GB-PVR), " +
  258.                 usedgbpvrpercent.ToString("#0.00") + "%";
  259.             imgFree.Attributes["title"] =
  260.                 free.ToString("0.0") + " " + freeunit + " Free, " +
  261.                 freepercent.ToString("#0.00") + "%";
  262.         }
  263.  
  264.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  265.         [return: MarshalAs(UnmanagedType.Bool)]
  266.         static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
  267.         out ulong lpFreeBytesAvailable,
  268.         out ulong lpTotalNumberOfBytes,
  269.         out ulong lpTotalNumberOfFreeBytes);
  270.  
  271.         private void ReduceSIUnit(ref double value, ref string unit)
  272.         {
  273.             int unitpos = 0;
  274.             string[] units = new string[] {"", "K", "M", "G", "T", "P"};
  275.             while (value > 1024) { value = value / 1024; unitpos++; }
  276.             unit = units[unitpos] + unit;
  277.         }
  278.  
  279.         private long GetDirectorySize(string pathName)
  280.         {
  281.             long totalsize = 0;
  282.             FileSystemInfo[] infos = (new DirectoryInfo(pathName)).GetFileSystemInfos();
  283.             foreach (FileSystemInfo info in infos)
  284.             {
  285.                 long itemsize = 0;
  286.                 try
  287.                 {
  288.                     if (info is FileInfo)
  289.                     {
  290.                         itemsize = (info as FileInfo).Length;
  291.                     }
  292.                     else if (info is DirectoryInfo)
  293.                     {
  294.                         itemsize = GetDirectorySize((info as DirectoryInfo).FullName);
  295.                     }
  296.                 }
  297.                 catch (Exception ex)
  298.                 {
  299.                     Logger.Error(ex.ToString());
  300.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  301.                 }
  302.                 totalsize += itemsize;
  303.             }
  304.             return totalsize;
  305.         }
  306.         #endregion
  307.  
  308.         #region Schedule Status
  309.         private string GetScheduleStatus(IList myScheduledRecordings, string name, int status)
  310.         {
  311.             int count = 0;
  312.             foreach (ScheduledRecording scheduledRecording in myScheduledRecordings)
  313.             {
  314.                 if (scheduledRecording.getRecordingStatus() == status) count++;
  315.             }
  316.             return count + " " + name;
  317.         }
  318.         #endregion
  319.  
  320.         #region Recording Status
  321.         private void GetRecordingStatuses()
  322.         {
  323.             string[] statuses = GetRecordingService().getTunerStatus();
  324.             rptRecordingSource.DataSource = statuses;
  325.             rptRecordingSource.DataBind();
  326.         }
  327.  
  328.         protected string GetSourceName(RepeaterItem Container)
  329.         {
  330.             string value = Container.DataItem.ToString();
  331.             value = value.Substring(0, value.IndexOf(":"));
  332.             if (value == "My Source Name")
  333.                 return "Capture Source #" + (Container.ItemIndex + 1);
  334.             return value;
  335.         }
  336.  
  337.         protected string GetSourceStatus(RepeaterItem Container)
  338.         {
  339.             string value = Container.DataItem.ToString();
  340.             value = value.Substring(value.IndexOf(":") + 1);
  341.             if (liveTVRegex.IsMatch(Container.DataItem.ToString()))
  342.             {
  343.                 value =
  344.                     "<a href=\"" + Download.GetDownloadUrl(false, false, Download.InternalFiles.LiveTV, Container.ItemIndex.ToString()) + "\" >" + value + "</a>";
  345.             }
  346.             return value;
  347.         }
  348.         #endregion
  349.  
  350.         #region EPG Remaining
  351.  
  352.         string GetEPGRemaining(DbConnection aConnection)
  353.         {
  354.             // create the command object and store the sql query
  355.             string SQLstr = "SELECT MAX(start_time) FROM PROGRAMME";
  356.             DbCommand aCommand = aConnection.CreateCommand();
  357.             aCommand.CommandText = SQLstr;
  358.             aCommand.Connection = aConnection;
  359.  
  360.             //create the datareader object to connect to table
  361.             DbDataReader aReader = aCommand.ExecuteReader();
  362.  
  363.             DateTime maxDate = DateTime.MinValue;
  364.             while(aReader.Read())
  365.             {
  366.                 maxDate = aReader.GetDateTime(0);
  367.             }
  368.             //close the reader
  369.             aReader.Close();
  370.  
  371.             if (maxDate != DateTime.MinValue)
  372.             {
  373.                 TimeSpan span = maxDate.Subtract(DateTime.Today);
  374.                 return "You have guide data until " + maxDate.ToShortDateString() +
  375.                     " (" + span.TotalDays.ToString("0") + " days)";
  376.             }
  377.             else
  378.             {
  379.                 throw new DataException();
  380.             }
  381.         }
  382.  
  383.         #endregion
  384.  
  385.         #region Current Transcode
  386.  
  387.         string GetCurrentTranscode(DbConnection aConnection)
  388.         {
  389.             // create the command object and store the sql query
  390.             string SQLstr = "SELECT t.percentage, r.filename FROM TRANSCODE_QUEUE t, " +
  391.                 "RECORDING_SCHEDULE r WHERE (r.oid = t.scheduled_recording_oid) " +
  392.                 "AND (t.status = 1)";
  393.             DbCommand aCommand = aConnection.CreateCommand();
  394.             aCommand.CommandText = SQLstr;
  395.             aCommand.Connection = aConnection;
  396.  
  397.             //create the datareader object to connect to table
  398.             DbDataReader aReader = aCommand.ExecuteReader();
  399.  
  400.             string labelText = string.Empty;
  401.             while(aReader.Read())
  402.             {
  403.                 int percentage = aReader.GetInt32(0);
  404.                 string filename = aReader.GetString(1);
  405.  
  406.                 if (percentage > 100)
  407.                     percentage = 100;
  408.  
  409.                 filename = Path.GetFileNameWithoutExtension(filename);
  410.  
  411.                 if (labelText.Length > 0)
  412.                     labelText = labelText + "<br/>";
  413.  
  414.                 labelText = labelText + filename + " : " + percentage + "% Complete";
  415.             }
  416.  
  417.             //close the reader
  418.             aReader.Close();
  419.  
  420.             if (labelText.Length > 0)
  421.                 return labelText;
  422.             return "There are currently no files being transcoded";
  423.         }
  424.  
  425.         #endregion
  426.  
  427.         #region Web Form Designer generated code
  428.         override protected void OnInit(EventArgs e)
  429.         {
  430.             //
  431.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  432.             //
  433.             InitializeComponent();
  434.             base.OnInit(e);
  435.         }
  436.  
  437.         /// <summary>
  438.         /// Required method for Designer support - do not modify
  439.         /// the contents of this method with the code editor.
  440.         /// </summary>
  441.         private void InitializeComponent()
  442.         {
  443.  
  444.         }
  445.         #endregion
  446.  
  447.     }
  448. }
  449.